clear all 
set more off 
set maxvar 15000 
clear matrix


    use "$Mydirectory2/appendix_d/DM_sample_foranalysis.dta", clear

    levelsof byear, local(levels) 
    di "`levels'"

*-----------------------------------------------------*
* Run regressions *
*-----------------------------------------------------*
/*Notes: (1) The "common sample" = respondents with 
             non-missing actual income in both 
             generations and non-missing predicted 
             working father income.
*/

* 1. Actual parental income, common sample
    preserve
        keep if comb_sample==1 
        
        matrix ige_DMcomb_EJreg= J(10,3,.)
        local i =0  

        foreach y in `levels'  {
            reg lfaminc1 lfaminc0 if byear==`y' [w=weight], cluster(hhid)
            
            local i=`i'+1
            matrix ige_DMcomb_EJreg[`i',1] = `y'
            matrix ige_DMcomb_EJreg[`i',2] = _b[lfaminc0]
            matrix ige_DMcomb_EJreg[`i',3] = _se[lfaminc0]
        }
    restore

* 2. Predicted father income, common sample
    preserve 
        keep if comb_sample==1 
        
        matrix ige_ejcomb_EJreg= J(10,3,.)
        local i =0

        foreach y in `levels' {
            reg lfaminc1 log_father_interpolated_retro if byear==`y' [w=weight], cluster(hhid)
        
            local i=`i'+1
            matrix ige_ejcomb_EJreg[`i',1] = `y'
            matrix ige_ejcomb_EJreg[`i',2] = _b[log_father_interpolated_retro]
            matrix ige_ejcomb_EJreg[`i',3] = _se[log_father_interpolated_retro]
        }
    restore 

*-----------------------------------------------*
* Make figures
*-----------------------------------------------*

    //Figure 1: Overlay (1) and (2) 
    clear 
    svmat ige_DMcomb_EJreg
    ren ige_DMcomb_EJreg1 year
    gen l95_DMcomb_EJreg = ige_DMcomb_EJreg2-1.96*ige_DMcomb_EJreg3
    gen u95_DMcomb_EJreg = ige_DMcomb_EJreg2+1.96*ige_DMcomb_EJreg3
    sort year
    tempfile ige_overlay_EJreg
    save `ige_overlay_EJreg'

    clear 
    svmat ige_ejcomb_EJreg
    ren ige_ejcomb_EJreg1 year
    gen l95_ejcomb_EJreg = ige_ejcomb_EJreg2-1.96*ige_ejcomb_EJreg3
    gen u95_ejcomb_EJreg = ige_ejcomb_EJreg2+1.96*ige_ejcomb_EJreg3
    sort year

    merge 1:1 year using `ige_overlay_EJreg'
    drop _merge

    gen year2 = year + 0.15

    preserve 
        keep if year<=1960 //birth cohorts up to 1960
        #delimit ;
        twoway (connect ige_DMcomb_EJreg2 year, msymbol(circle_hollow) mcolor(blue) msize(medium) lcolor(blue) lwidth(0.25) lpat(solid)) (rcap u95_DMcomb_EJreg l95_DMcomb_EJreg year, lcolor(blue) lwidth(0.25))
               (connect ige_ejcomb_EJreg2 year2, msymbol(square) mcolor(stone*1.25) msize(medium) lcolor(stone*1.25) lwidth(0.25) lpat(solid)) (rcap u95_ejcomb_EJreg l95_ejcomb_EJreg year2, lcolor(stone*1.25) lwidth(0.25)),        
        graphregion(color(white)) legend(on ring(0) row(2) pos(11) order(1 "Actual parental income" 3 "Predicted parental income")) 
        xtitle(" " "Birth Year") ytitle("IGE" " ", axis(1)) ylabel(0(0.2)1,nogrid axis(1)) xscale(range(1947.5 1953.5))
        ;
        #delimit cr
        graph export "$Mydirectory2/appendix_d/ige_overlay_mdej_EJreg_upto1960.pdf", as(pdf) replace

    restore
